<?php
defined('IN_TS') or die('Access Denied.');

//http://localhost/thinksaas_www/index.php?app=study&ac=caiji&channel=php&cateid1=27&cateid2=28&total=2&pn=1

$channel = trim($_GET['channel']);  //好知的课程频道
$total = trim($_GET['total']);  //总页数

$cateid1 = intval($_GET['cateid1']);    //对应的1级分类
$cateid2 = intval($_GET['cateid2']);    //对应的2级分类

$pn = intval($_GET['pn']);      //分页

if($channel=='' || $total=='' || $cateid1=='' || $cateid2=='' || $pn==''){
    echo tsUrl('study', 'caiji', array('channel'=>$channel,'cateid1'=>$cateid1,'cateid2'=>$cateid2,'total'=>$total,'pn' =>
        $pn + 1));
    exit;
}


function compress_html($string) {
    $string = str_replace("\r\n", '<br />', $string); //清除换行符
    $string = str_replace("\n", '<br />', $string); //清除换行符
    $string = str_replace("\t", '<br />', $string); //清除制表符
    $pattern = array (
        "/> *([^ ]*) *</", //去掉注释标记
        "/[\s]+/",
        "/<!--[\\w\\W\r\\n]*?-->/",
        "/\" /",
        "/ \"/",
        "'/\*[^*]*\*/'"
    );
    $replace = array (
        ">\\1<",
        " ",
        "",
        "\"",
        "\"",
        ""
    );
    return preg_replace($pattern, $replace, $string);
}

$arrUser = array(25864,25865,25866,25867,25868,25869,25870,25871,25872,25873,25874,25875,25876,25877,25878,25879,25880,25881,25882,25883,25884,25885,25886,25887,25888,25889,25890,25891,25892,25893,25894,25895,25896,25897,25898,25899,25900,25901,25902,25903,25904,25905,25906,25907,25908,25909,25910,25911,25912,25913,25914,25915,25916,25917,25918,25919,25920,25921,25922,25923,25924,25925,25926,25927,25928,25929,25930,25931,25932,25933,25934,25935,25936,25937,25938,25939,25940,25941,25942,25943,25944,25945,25946,25947,25948,25949,25950,25951,25952,25953,25954,25955,25956,25957,25958,25959,25960,25961,25962,25963,25964,25965,25966,25967,25968,25969,25970,25971,25972,25973,25974,25975,25976,25977,25978,25979,25980,25981,25982,25983,25984,25985,25986,25987,25988,25989,25990,25991,25992,25993,25994,25995,25996,25997,25998,25999,26000,26001,26002,26003,26004,26005,26006,26007,26008,26009,26010,26011,26012,26013,26014,26015,26016,26017,26018,26019,26020,26021,26022,26023,26024,26025,26026,26027,26028,26029,26030,26031,26032,26033,26034,26035,26036,26037,26038,26039,26040,26041,26042,26043,26044,26045,26046,26047,26048,26049,26050,26051,26052,26053,26054,26055,26056,26057,26058,26059,26060,26061,26062,26063,26064,26065,26066,26067,26068,26069,26070,26071,26072,26073,26074,26075,26076,26077,26078,26079,26080,26081,26082,26083,26084,26085,26086,26087,26088,26089,26090,26091,26092,26093,26094,26095,26096,26097,26098,26099,26100,26101,26102,26103,26104,26105,26106,26107,26108,26109,26110,26111,26112,26113,26114,26115,26116,26117,26118,26119,26120,26121,26122,26123,26124,26125,26126,26127,26128,26129,26130,26131,26132,26133,26134,26135,26136,26137,26138,26139,26140,26141,26142,26143,26144,26145,26146,26147,26148,26149,26150,26151,26152,26153,26154,26155,26156,26157,26158,26159,26160,26161,26162,26163,26164,26165,26166,26167,26168,26169,26170,26171,26172,26173,26174,26175,26176,26177,26178,26179,26180,26181,26182,26183,26184,26185,26186,26187,26188,26189,26190,26191,26192,26193,26194,26195,26196,26197,26198,26199,26200,26201,26202,26203,26204,26205,26206,26207,26208,26209,26210,26211,26212,26213,26214,26215,26216,26217,26218,26219,26220,26221,26222,26223,26224,26225,26226,26227,26228,26229,26230,26231,26232,26233,26234,26235,26236,26237,26238,26239,26240,26241,26242,26243,26244,26245,26246,26247,26248,26249,26250,26251,26252,26253,26254,26255,26256,26257,26258,26259,26260,26261,26262,26263,26264,26265,26266,26267,26268,26269,26270,26271,26272,26273,26274,26275,26276,26277,26278,26279,26280,26281,26282,26283,26284,26285,26286,26287,26288,26289,26290,26291,26292,26293,26294,26295,26296,26297,26298,26299,26300,26301,26302,26303,26304,26305,26306,26307,26308,26309,26310,26311,26312,26313,26314,26315,26316,26317,26318,26319,26320,26321,26322,26323,26324);


require 'thinksaas/QueryList.class.php';


//$list = file_get_contents('http://www.howzhi.com/channel/php?page='.$pn);
$list = file_get_contents('http://www.howzhi.com/channel/'.$channel.'?page='.$pn);

//echo $list;exit;

preg_match_all('|<a class="transition" href="(.*?)">.*?</a>|U', $list, $arrUrl, PREG_SET_ORDER);

//print_r($arrUrl);exit;

foreach($arrUrl as $key=>$item){
    $url = 'http://www.howzhi.com'.$item['1'];
    //$url = 'http://www.howzhi.com/course/10510/';
    //$page = file_get_contents($url);
    //$page2 = _prefilter(file_get_contents($url));
    //echo $page;exit;


    //采OSC内容页内容
    $reg = array("title"=>array("h1","text"),"content"=>array(".hz-line-left","html"),"photo"=>array(".pic .img-responsive","src"));
    $hj = QueryList::Query($url,$reg);
    $arr = $hj->jsonArr;

    $title = $arr[0]['title'];
    $content = $arr[0]['content'];

    $photo = $arr[0]['photo'];

    //preg_match_all('|<a href="/course/.*?/lesson/(.*?)">.*?</a></span>|U', $page, $arrLesson, PREG_SET_ORDER);


    //print_r($arrLesson);exit;



    $content = str_replace('img src="/','img src="http://www.howzhi.com/',$content);
    $content = tsCleanContent($content);


    if($title && $content && $photo){

        //马甲
        $ukey = array_rand($arrUser);
        $userid = $arrUser[$ukey];
        //$userid = 1;


        $strStudy = $new['study']->find('study',array(
            'title'=>$title,
        ));

        if($strStudy){


            //更新封面图
            $arrUpload = tsUploadPhotoUrl($photo,$strStudy['studyid'],'study');
            if($arrUpload){
                $new['study']->update('study',array(
                    'studyid'=>$strStudy['studyid'],
                ),array(
                    'path'=>$arrUpload['path'],
                    'photo'=>$arrUpload['url'],
                ));
            }


        }else{
            //创建课程
            $studyid = $new['study']->create('study',array(
                'userid'=>$userid,
                'studyname'=>$title,
                'studydesc'=>$content,

                'fromurl'=>$url,

                'price'=>0,

                'cateid'=>$cateid1,
                'cateid2'=>$cateid2,
                'ispublish'=>0,    //未发布状态
                'isaudit'=>0,			//未审核状态
                'addtime'=>time(),
                'uptime'=>time(),

            ));

            //更新封面图
            $arrUpload = tsUploadPhotoUrl($photo,$studyid,'study');
            if($arrUpload){
                $new['study']->update('study',array(
                    'studyid'=>$studyid,
                ),array(
                    'path'=>$arrUpload['path'],
                    'photo'=>$arrUpload['url'],
                ));
            }


            //创建课时
            /*
            if($arrLesson){
                foreach($arrLesson as $lkey=>$litem){

                    $lesson = 'http://www.howzhi.com'.$item[1].'lesson/'.$litem[1];
                    //$lesson = 'http://www.howzhi.com/course/2522/lesson/35410';
                    $lessonpage = file_get_contents($lesson);
                    $lessonpage2 = _prefilter(file_get_contents($lesson));
                    $lessonpage3 = compress_html(file_get_contents($lesson));

                    //echo $lessonpage3;exit;

                    preg_match_all('|<h1><span class="lesson-index">.*?</span><span class="lesson-title">(.*?)</span></h1>|U', $lessonpage, $arrLessonTitle, PREG_SET_ORDER);

                    //print_r($arrLessonTitle);exit;

                    preg_match_all('|<div class="editor-content">(.*?)</div><br /><div class="around-nav tar">|U', $lessonpage3, $arrLessonContent, PREG_SET_ORDER);

                    //print_r($arrLessonContent);exit;

                    $lessonTitle = trim($arrLessonTitle[0][1]);


                    $lessonContent = tsCleanContent(br2nl(trim($arrLessonContent[0][1])));

                    //echo $lessonContent;exit;

                    $lessonid = $new['study']->create('study_lesson',array(
                        'studyid' => $studyid,
                        'userid' => $userid,
                        'orderid' => $lkey+1,
                        'title' => $lessonTitle,
                        'content' => $lessonContent,
                        'addtime' => time(),
                    ));

                }


                //统计总课时
                $count_lesson = $new['study']->findCount('study_lesson',array(
                    'studyid'=>$studyid,
                ));
                $new['study']->update('study',array(
                    'studyid'=>$studyid,
                ),array(
                    'count_lesson'=>$count_lesson,
                ));

            }
            */
        }






    }

}

if($pn<=$total) {
    echo '<a href="' . tsUrl('study', 'caiji', array('channel'=>$channel,'cateid1'=>$cateid1,'cateid2'=>$cateid2,'total'=>$total,'pn' =>
            $pn + 1)) . '">下一页</a>';
}